//
//  SubViewController.m
//  Shadeco
//
//  Created by Le Thanh Hai on 9/13/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MenuViewController.h"
#import "ContentViewController.h"


@implementation MenuViewController
@synthesize shadeco;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
	[btnFrame   release];
    [btnStamp   release];
	[btnPen     release];
	[btnText    release];
	[btnBrush   release];
	[btnColor   release];
	[btnLib     release];
	[btnCamera  release];
	[btnSave    release];
	[btnMail    release];
	[btnNone    release];
	[btnLanguage release];
    [lblCompany release];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

- (void)frameSelected: (id)sender
{     
    NSLog(@"%@", @"Frame Selecte");
    if (frameController == nil) {
		frameController = [[FrameView alloc] init];
		[contentView addSubview: frameController.view];
		frameController.view.frame = CGRectMake(0, 110, 320, 360);
        [stampController.view removeFromSuperview];
	} else
    {
		[contentView addSubview: frameController.view];
    }
}

- (void)stampSelected: (id)sender
{
    NSLog(@"%@", @"Stamp Selected");
    if (stampController == nil) {
		stampController = [[StampView alloc] init];
		[contentView addSubview: stampController.view];
		stampController.view.frame = CGRectMake(0, 110, 320, 360);
        [frameController.view removeFromSuperview];
	} else
    {
		[contentView addSubview: stampController.view];
    }
}

- (void)setupButton
{
	// Add Button Frame
    btnFrame = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnFrame.frame = CGRectMake(2, 19, 52, 40);
    [btnFrame setBackgroundImage:[UIImage imageNamed:@"Frame.png"] forState:UIControlStateNormal];
    [btnFrame addTarget:self action:@selector(frameSelected:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:btnFrame];
    
    // Add Button Stamp
    btnStamp = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnStamp.frame = CGRectMake(54, 19, 52, 40);
    [btnStamp setBackgroundImage:[UIImage imageNamed:@"Stamp.png"] forState:UIControlStateNormal];
    [btnStamp addTarget:self action:@selector(stampSelected:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:btnStamp];
    
    // Add Button Pen
    btnPen = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnPen.frame = CGRectMake(106, 19, 52, 40);
    [btnPen setBackgroundImage:[UIImage imageNamed:@"Pen.png"] forState:UIControlStateNormal];
    [btnPen addTarget:shadeco action:@selector(PenSelected:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:btnPen];
    
    // Add Button Text
    btnText = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnText.frame = CGRectMake(158, 19, 52, 40);
    [btnText setBackgroundImage:[UIImage imageNamed:@"Text.png"] forState:UIControlStateNormal];
    [btnText addTarget:shadeco action:@selector(TextSelected:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:btnText];
    
    // Add Button Brush
    btnBrush = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnBrush.frame = CGRectMake(210, 19, 52, 40);
    [btnBrush setBackgroundImage:[UIImage imageNamed:@"Brush.png"] forState:UIControlStateNormal];
    [btnBrush addTarget:shadeco action:@selector(BrushSelected:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:btnBrush];
    
    // Add Button Color
    btnColor = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnColor.frame = CGRectMake(262, 19, 52, 40);
    [btnColor setBackgroundImage:[UIImage imageNamed:@"ColorOne.png"] forState:UIControlStateNormal];
    [btnColor addTarget:shadeco action:@selector(ColorSelected:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:btnColor];
    
    // Add Button Library
    btnLib = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnLib.frame = CGRectMake(2, 61, 52, 40);
    [btnLib setBackgroundImage:[UIImage imageNamed:@"Lib.png"] forState:UIControlStateNormal];
    [btnLib addTarget:shadeco action:@selector(LibSelected:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:btnLib];
    
    // Add Button Camera
    btnCamera = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnCamera.frame = CGRectMake(54, 61, 52, 40);
    [btnCamera setBackgroundImage:[UIImage imageNamed:@"Kamera.png"] forState:UIControlStateNormal];
    [btnCamera addTarget:shadeco action:@selector(CameraSelected:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:btnCamera];
    
    // Add Button Save
    btnSave = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnSave.frame = CGRectMake(106, 61, 52, 40);
    [btnSave setBackgroundImage:[UIImage imageNamed:@"Save.png"] forState:UIControlStateNormal];
    [btnSave addTarget:shadeco action:@selector(SaveSelected:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:btnSave];
    
    // Add Button Mail
    btnMail = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnMail.frame = CGRectMake(158, 61, 52, 40);
    [btnMail setBackgroundImage:[UIImage imageNamed:@"Mail.png"] forState:UIControlStateNormal];
    [btnMail addTarget:shadeco action:@selector(MailSelected:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:btnMail];
    
    // Add Button None
    btnNone = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnNone.frame = CGRectMake(210, 61, 52, 40);
    [btnNone setBackgroundImage:[UIImage imageNamed:@"None.png"] forState:UIControlStateNormal];
    [contentView addSubview:btnNone];
    
    // Add Button Language
    btnLanguage = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnLanguage.frame = CGRectMake(262, 61, 52, 40);
    [btnLanguage setBackgroundImage:[UIImage imageNamed:@"Lang.png"] forState:UIControlStateNormal];
    [btnLanguage addTarget:shadeco action:@selector(LanguageSelected:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:btnLanguage];
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];  
    // init variable
    flag = NO;
    
    // create notify in order to hide view menu    
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideView:) name:@"HideViewMenu" object:nil];	
    
    // Add content view
    contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    contentView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    [self.view addSubview:contentView];
    [contentView release];
    
    UIImageView *introduceView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"introduce.png"]]autorelease];
    [contentView addSubview:introduceView];
    introduceView.frame = CGRectMake(3, 115, 314, 362);
    
    // Add label Company and product
    lblCompany = [[UILabel alloc] init];
    lblCompany.frame = CGRectMake(10, 1, 150, 19);
    lblCompany.font = [UIFont fontWithName:@"AppleGothic" size:14];
    lblCompany.backgroundColor = [UIColor clearColor];
    lblCompany.text = @"ShaDeco ver1.0";
    lblCompany.textColor = [UIColor lightTextColor];
    [contentView addSubview:lblCompany];
    //[lblCompany release];
    lblProduct = [[UILabel alloc] init];
    lblProduct.frame = CGRectMake(290, 1, 30, 19);
    lblProduct.font = [UIFont fontWithName:@"AppleGothic" size:14];
    lblProduct.backgroundColor = [UIColor clearColor];
    lblProduct.text = @"JIT";
    lblProduct.textColor = [UIColor lightTextColor];
    [contentView addSubview:lblProduct];
    [lblProduct release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    btnFrame    = nil;
    btnStamp    = nil;
    btnPen      = nil;
    btnText     = nil;
    btnBrush    = nil;
    btnColor    = nil;
    btnLib      = nil;
    btnCamera   = nil;
    btnSave     = nil;
    btnMail     = nil;
    btnNone     = nil;
    btnLanguage = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    // return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return YES;
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.1];
	[UIView setAnimationTransition: UIViewAnimationTransitionNone forView:self.view cache:YES];
    
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        contentView.frame   = CGRectMake(0, 0, 480, 320);
        btnFrame.frame      = CGRectMake(2, 19, 52, 40);       
        btnStamp.frame      = CGRectMake(2, 61, 52, 40);     
        btnPen.frame        = CGRectMake(2, 103, 52, 40);     
        btnText.frame       = CGRectMake(2, 145, 52, 40);     
        btnBrush.frame      = CGRectMake(2, 187, 52, 40);     
        btnColor.frame      = CGRectMake(2, 229, 52, 40);     
        btnLib.frame        = CGRectMake(54, 19, 52, 40);     
        btnCamera.frame     = CGRectMake(54, 61, 52, 40);     
        btnSave.frame       = CGRectMake(54, 103, 52, 40);     
        btnMail.frame       = CGRectMake(54, 145, 52, 40);     
        btnNone.frame       = CGRectMake(54, 187, 52, 40);     
        btnLanguage.frame   = CGRectMake(54, 229, 52, 40);   
        lblCompany.frame    = CGRectMake(5, 275, 150, 19);
        lblProduct.frame    = CGRectMake(40, 295, 30, 19); 		
		frameController.view.frame = CGRectMake(130, -15, 320, 360);
		stampController.view.frame = CGRectMake(130, -15, 320, 360);
		UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(455, 10, 20, 120)];
		lb.numberOfLines = 10;
		lb.backgroundColor = [UIColor clearColor];
		lb.textColor = [UIColor whiteColor];
		lb.font = [UIFont fontWithName:@"AppleGothic" size:12];
		lb.textAlignment = UITextAlignmentCenter;
		lb.text = @"フ\nレ\n|\nム\nツ\n|\nル";
		[contentView addSubview: lb];
		[lb release];
    }
    else
    {
        contentView.frame   = CGRectMake(0, 0, 320, 480);
        btnFrame.frame      = CGRectMake(2, 19, 52, 40);
        btnStamp.frame      = CGRectMake(54, 19, 52, 40);
        btnPen.frame        = CGRectMake(106, 19, 52, 40);
        btnText.frame       = CGRectMake(158, 19, 52, 40);
        btnBrush.frame      = CGRectMake(210, 19, 52, 40);
        btnColor.frame      = CGRectMake(262, 19, 52, 40);
        btnLib.frame        = CGRectMake(2, 61, 52, 40);
        btnCamera.frame     = CGRectMake(54, 61, 52, 40);
        btnSave.frame       = CGRectMake(106, 61, 52, 40);
        btnMail.frame       = CGRectMake(158, 61, 52, 40);
        btnNone.frame       = CGRectMake(210, 61, 52, 40);
        btnLanguage.frame   = CGRectMake(262, 61, 52, 40);
        lblCompany.frame    = CGRectMake(10, 1, 150, 19);
        lblProduct.frame    = CGRectMake(290, 1, 30, 19);
		frameController.view.frame = CGRectMake(0, 110, 320, 360);
		stampController.view.frame = CGRectMake(0, 110, 320, 360);
    }
    [UIView commitAnimations];
}

-(void)hideView:(NSNotification *)notification
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [self.view setAlpha:(float)flag];
    flag = !flag;
}

@end
